package View.apply;

import Service.ChangeDormAppService;
import Service.ServiceImpl.ChangeDormAppServiceImpl;
import Service.ServiceImpl.systemMsgServiceImpl;
import Service.systemMsgService;
import domain.ChangeDormApp;
import domain.User;
import domain.systemMsg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class ChangeDormAppApprove extends JFrame {
    public ChangeDormAppApprove(ChangeDormApp app, User user){
        init();
        componentInit(app,user);
    }

    public void init(){
        this.setTitle("更换宿舍审批");
        this.setSize(432,550);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }

    public void componentInit(ChangeDormApp app, User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("学号:");
        label1.setBounds(104,18,65,30);
        JLabel label2= new JLabel();
        label2.setText("姓名:");
        label2.setBounds(105,72,65,30);
        JLabel label3= new JLabel();
        label3.setText("宿舍号:");
        label3.setBounds(107,127,65,30);
        JLabel label4= new JLabel();
        label4.setText("与你交换的学生号:");
        label4.setBounds(55,176,117,30);
        JLabel label5= new JLabel();
        label5.setText("与你交换的学生姓名:");
        label5.setBounds(56,232,116,30);
        JLabel label6= new JLabel();
        label6.setText("与你交换的学生的宿舍号:");
        label6.setBounds(21,281,151,30);
        JLabel label7= new JLabel();
        label7.setText("审核人:");
        label7.setBounds(107,400,65,30);
        JLabel label8= new JLabel();
        label8.setText("申请原因:");
        label8.setBounds(103,331,65,30);

        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText(app.getStunum());
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(194,19,120,30);
        JTextField field2= new JTextField();
        field2.setText(app.getName());
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(195,72,120,30);
        JTextField field3= new JTextField();
        field3.setText(app.getDormnum());
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(197,130,120,30);
        JTextField field4= new JTextField();
        field4.setText(app.getChangestunum());
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(198,179,120,30);
        JTextField field5= new JTextField();
        field5.setText(app.getChangename());
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(199,231,120,30);
        JTextField field6= new JTextField();
        field6.setText(app.getChangedormnum());
        field6.setBorder(new LineBorder(Color.lightGray));
        field6.setBounds(198,284,120,30);
        JTextField field7= new JTextField();
        field7.setText("");
        field7.setBorder(new LineBorder(Color.lightGray));
        field7.setBounds(203,400,120,30);
        JTextArea field8= new JTextArea();
        field8.setText(app.getAppReason());
        field8.setBorder(new LineBorder(Color.lightGray));
        field8.setBounds(199,331,158,51);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("通过");
        button1.setBounds(26,482,95,27);
        JButton button2= new JButton();
        button2.setText("不通过");
        button2.setBounds(157,481,95,27);
        JButton button3= new JButton();
        button3.setText("退出");
        button3.setBounds(282,479,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(label8);
        this.add(field1);
        field1.setEnabled(false);
        this.add(field2);
        field2.setEnabled(false);
        this.add(field3);
        field3.setEnabled(false);
        this.add(field4);
        field4.setEnabled(false);
        this.add(field5);
        field5.setEnabled(false);
        this.add(field6);
        field6.setEnabled(false);
        this.add(field7);
        this.add(field8);
        field8.setEnabled(false);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ChangeDormAppService service = new ChangeDormAppServiceImpl();
                service.changeApply(app.getId(),"true",field7.getText());

                systemMsg msg = new systemMsg();
                msg.setUsername(app.getName());
                msg.setTitle("更换宿舍申请");
                msg.setNeirong(app.getName()+"你的更换宿舍申请已通过");
                msg.setSendTime(new Date());
                msg.setIsread("false");
                systemMsgService service1 = new systemMsgServiceImpl();
                service1.addMsg(msg);

                msg.setUsername(app.getChangename());
                msg.setIsread("更换宿舍申请");
                msg.setNeirong(app.getChangename()+"你的更换宿舍申请已通过");
                msg.setSendTime(new Date());
                msg.setIsread("false");
                service1.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"审核结果提交成功!");
                dispose();
                new ChangeDormapp(user).setVisible(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ChangeDormAppService service = new ChangeDormAppServiceImpl();
                service.changeApply(app.getId(),"false",field7.getText());

                systemMsg msg = new systemMsg();
                msg.setUsername(app.getName());
                msg.setTitle("更换宿舍申请");
                msg.setNeirong(app.getName()+"你的更换宿舍申请未通过");
                msg.setSendTime(new Date());
                msg.setIsread("false");
                systemMsgService service1 = new systemMsgServiceImpl();
                service1.addMsg(msg);

                msg.setUsername(app.getChangename());
                msg.setIsread("更换宿舍申请");
                msg.setNeirong(app.getChangename()+"你的更换宿舍申请未通过");
                msg.setSendTime(new Date());
                msg.setIsread("false");
                service1.addMsg(msg);

                JOptionPane.showMessageDialog(new JFrame(),"审核结果提交成功!");
                dispose();
                new ChangeDormapp(user).setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new ChangeDormapp(user).setVisible(true);
            }
        });
    }
}
